package cn.tedu.store.admin.webapi.service.impl;


import cn.tedu.store.commons.ex.ServiceException;
import cn.tedu.store.commons.pojo.admin.dto.RolePermissionAddNewDTO;
import cn.tedu.store.commons.pojo.admin.entity.RolePermission;
import cn.tedu.store.commons.pojo.admin.vo.RolePermissionListItemVO;
import cn.tedu.store.commons.pojo.admin.vo.RolePermissionStandardVO;
import cn.tedu.store.commons.web.ServiceCode;
import cn.tedu.store.admin.webapi.mapper.RolePermissionMapper;
import cn.tedu.store.admin.service.IRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RolePermissionServiceImpl implements IRolePermissionService {

    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Override
    public void addNew(RolePermissionAddNewDTO rolePermissionAddNewDTO) {
        int count = rolePermissionMapper.countByRoleIdPermission(rolePermissionAddNewDTO.getRoleId(), rolePermissionAddNewDTO.getPermissionId());
        if (count != 0) {
            String message = "添加角色关联信息失败,该关联信息已存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        RolePermission rolePermission = new RolePermission();
        BeanUtils.copyProperties(rolePermissionAddNewDTO, rolePermission);
        rolePermissionMapper.insert(rolePermission);
        log.debug("添加管理员成功!");
    }

    @Override
    public void delete(Long id) {
        int count = rolePermissionMapper.countById(id);
        if (count == 0) {
            String message = "删除该信息失败,该信息不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        rolePermissionMapper.deleteById(id);
        log.debug("删除角色成功!");
    }

    @Override
    public void updateById(Long id, RolePermissionAddNewDTO rolePermissionAddNewDTO) {
        log.debug("即将处理修改角色业务!");
        RolePermissionStandardVO standard = rolePermissionMapper.getStandardById(id);
        if (standard == null) {
            String message = "修改关联信息失败,该关联信息不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        RolePermission rolePermission = new RolePermission();
        BeanUtils.copyProperties(rolePermissionAddNewDTO, rolePermission);
        rolePermission.setId(id);
        log.debug("即将修改的数据: {}", rolePermission);
        int count = rolePermissionMapper.updateById(rolePermission);
        if (count != 1){
            String message = "修改角色失败,服务器忙,请稍后重试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public List<RolePermissionListItemVO> list() {
        log.debug("开始处理查询角色列表业务!");
        return rolePermissionMapper.list();
    }
}
